package com.heima.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.heima.bean.Dish;
import com.heima.common.PageParam;
import com.heima.common.R;
import com.heima.dto.DishDto;
import com.heima.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/dish")
@RestController
public class DishController {
    @Autowired
    private DishService dishService;

    /**
     * 根据菜品的分类获取具体的菜品数据
     * @param categoryId
     * @param status
     * @return
     */
    @GetMapping("/list")
    public R showByCategoryId(Long categoryId, Integer status){
        List<DishDto> dishList = dishService.findByCategory(categoryId, status);
        return R.success(dishList);
    }

    /**
     * 添加菜品
     * @param dishDto
     * @return
     */
    @PostMapping
    public R add(@RequestBody DishDto dishDto){
        int row = dishService.add(dishDto);
        if (row > 0) {
            return R.success("添加菜品成功！");
        }
        return R.success("添加菜品失败！");
    }

    /**
     * 按条件分页展示
     * @param pageParam
     * @return
     */
    @GetMapping("/page")
    public R page(PageParam pageParam){
        IPage<DishDto> dishIPage = dishService.findPage(pageParam);
        return R.success(dishIPage);
    }

    /**
     * 更新菜品
     * @param dishDto
     * @return
     */
    @PutMapping
    public R update(@RequestBody DishDto dishDto){
        int row = dishService.update(dishDto);
        if (row > 0) {
            return R.success("更新菜品成功！");
        }
        return R.error("更新菜品失败！");
    }
}
